package demo9;

/**
 * 最大的交替和
 */
public class Main {
    public static void main(String[] args) {
        int[] num = {1,-2,3,-4,5};  // 1 - (-2) + 3 - (-4) + 5
        System.out.println(help(num));
    }

    public static int help(int[] num){
        int[][] dp = new int[num.length][2];
        dp[0][0] = num[0];
        dp[0][1] = 0;
        for (int i = 1; i < num.length; i++) {
            dp[i][0] = Math.max(num[i],dp[i-1][1]+num[i]);
            dp[i][1] = Math.max(-num[i],dp[i-1][0]-num[i]);
        }
        return Math.max(dp[num.length-1][0],dp[num.length-1][1]);
    }
}
